/**
 * @file /include/alphadog_monitor/main_window.hpp
 *
 * @brief Qt based gui for alphadog_monitor.
 *
 * @date November 2010
 **/
#ifndef alphadog_monitor_MAIN_WINDOW_H
#define alphadog_monitor_MAIN_WINDOW_H

/*****************************************************************************
** Includes
*****************************************************************************/
#include <QMainWindow>
#include <QProcess>
#include <QTimer>
#include <QVector>
#include <QKeyEvent>
#include "ui_main_window.h"
#include "../alphadog_monitor/setting.h"
#include "../alphadog_monitor/graphplot.h"
#include "qcustomplot.h"
#include "qnode.hpp"
#include "qrviz.hpp"
#include <QFrame>

/*****************************************************************************
** Define
*****************************************************************************/
typedef enum JOINT
{
    JOINT_RF1,
    JOINT_RF2,
    JOINT_RF3,

    JOINT_LF1,
    JOINT_LF2,
    JOINT_LF3,

    JOINT_RB1,
    JOINT_RB2,
    JOINT_RB3,

    JOINT_LB1,
    JOINT_LB2,
    JOINT_LB3,

    JOINTS_ALL
} JOINT;

typedef enum LEG
{
    LEG_RF,
    LEG_LF,
    LEG_RB,
    LEG_LB,

    LEG_NUM
} LEG;

typedef struct _Joystick
{
    uint8_t mode;
    float p_des[2];
    float v_des[3];
    float bodyHeight;
    float paceHeight[2];
    uint8_t gait;
    uint8_t jump_triggle;
    uint8_t jump_type;
    uint8_t au_type;
} Joystick;

typedef struct _PathCmd
{
    float type;
    float cmd[3];
} PathCmd;

/*****************************************************************************
** Namespace
*****************************************************************************/

typedef enum
{
    BezierTrajectory,
    LSPBTrajectory,
    SplineTrajectory
}PlanType;

namespace alphadog_monitor {

/*****************************************************************************
** Interface [MainWindow]
*****************************************************************************/
/**
 * @brief Qt central, all operations relating to the view part here.
 */
class MainWindow : public QMainWindow {
Q_OBJECT

public:
	MainWindow(int argc, char** argv, QWidget *parent = 0);
	~MainWindow();

	void closeEvent(QCloseEvent *event); // Overloaded function
	void showNoMasterMessage();
    bool connectMaster(QString master_ip, QString ros_ip);
    void Display(void);

public Q_SLOTS:
	/******************************************
	** Auto-connections (connectSlotsByName())
	*******************************************/
	void on_actionAbout_triggered();

    /******************************************
    ** Manual connections
    *******************************************/
    void slot_show_image(int, QImage);
    void publishJoystickCmd(void);
    void slot_updateState(float*);

signals:
 void signalDisconnect();
 void Vision_Initial(int);

protected:
void keyPressEvent(QKeyEvent *event); //键盘按下事件
void keyReleaseEvent(QKeyEvent *event); //键盘松开事件

private:
    Ui::MainWindowDesign* ui;
    QNode* qnode;

    Setting * setinf;
    graphplot* graph_plot;
    QRviz *map_rviz = NULL;
    PlanType TrajType;
    QProcess process;
    QCustomPlot* LF_Plot;
    QCustomPlot* LB_Plot;
    QCustomPlot* RB_Plot;
    QCustomPlot* RF_Plot;
    QVector<double> LF_x, LF_y;
    QVector<double> LB_x, LB_y;
    QVector<double> RB_x, RB_y;
    QVector<double> RF_x, RF_y;
    bool EnableDispCmd = true;
    bool ctrlEnable = false;
    uint8_t mode_ctrl = 0;
    const float ang_bias = 1.309;

    // 遥控数据
    Joystick joystick;
    QString gait_type;
    QString _jump;
    QString _au_cmd;
    uint8_t cur_gait = 0;
    uint8_t cur_jump = 0;
    QTimer* joystick_timer;
    sensor_msgs::JointState joystick_cmd;
    bool key_press_trrigle = false;

    // 路径规划数据
    QVector<PathCmd> path_cmd;
    QVector<QFrame*> cmd_boxes;
    QVector<QVector<QLabel*>> cmd_data;
    sensor_msgs::JointState route_cmd;

    void ReadSettings();    // 读取设置信息
    void WriteSettings();   // 写入设置信息
    void InitWidget();      // 初始化控件
    void InitPathWidget();  // 初始化路径规划相关控件
    void clearPathCmd();    // 清空路径规划相关指令
    void addPathCmd();      // 添加路径规划相关指令
    void display_rviz();    // 显示rviz
    void JointCtrl(JOINT l, float data);
    void FootCtrl(LEG l, float x, float y, float z);
    void SetGaitMenu(void);
    void SetJumpMenu(void);
    void SetAUMenu(void);
    float correctYaw(float yaw);
};

}  // namespace alphadog_monitor

#endif // alphadog_monitor_MAIN_WINDOW_H
